﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class levelAction : CWebBase
{
    public CDatabase _db = new CDatabase();

    protected void Page_Load(object sender, EventArgs e)
    {
        // 어떤 action ?
        String action = CheckNull(Request["action"]);

        _db.GetConnection();

        // depth
        if (action == "getLevel") {
            String topId = CheckNull(Request["topId"]);
            ArrayList LevelList = new ArrayList();

            // 최상위
            if (isEmpty(topId)) {
                topId = "0";
            }

            topId = RemoveSpecialChars(topId);

            String sql = "Select LevelId, TopLevelId, LevelName, LevelSeq from ANALYZE_WORD_LEVEL where TopLevelId = '" + topId + "' and DelYn != 'Y' order by LevelSeq";
            SqlDataReader dr = _db.GetQueryResult(sql);

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Hashtable data = new Hashtable();
                    data.Add("LevelId", dr["LevelId"].ToString());
                    data.Add("TopLevelId", dr["TopLevelId"].ToString());
                    data.Add("LevelName", dr["LevelName"].ToString());
                    data.Add("LevelSeq", dr["LevelSeq"].ToString());
                    LevelList.Add(data);
                }
            }
            dr.Close();

            Response.AddHeader("'Content-type", "application/json");
            Response.Write(getJson(LevelList));
            return;
        }

        if (action == "modName") {
            Hashtable result = new Hashtable();
            String succ = "Y";
            String levelId = CheckNull(Request["levelId"]);
            String levelName = CheckNull(Request["name"]);
            String topId = CheckNull(Request["topId"]);
            String baseId = "";

            topId = RemoveSpecialChars(topId);
            levelId = RemoveSpecialChars(levelId);

            baseId = topId;
            // 최상위
            if (isEmpty(topId))
            {
                topId = "0";
                baseId = "001";
            }

            try
            {
                Boolean isDup = false;

                // 중복 체크
                if (!isEmpty(topId)) {
                    String sql = "select LevelId from ANALYZE_WORD_LEVEL where topLevelId = '" + topId + "' and LevelName = '" + levelName + "'";
                    SqlDataReader dr = _db.GetQueryResult(sql);
                    if (dr.HasRows)
                    {
                        succ = "Dup";
                    }
                    dr.Close();
                }

                if (!isDup)
                {
                    // 신규
                    if (isEmpty(levelId))
                    {
                        String iLevelId = "";
                        String sql = "select '00' + convert(varchar(9), isnull(max(convert(int, levelId)), 0) + 1) as lvid from ANALYZE_WORD_LEVEL where topLevelId = '" + topId + "'";
                        SqlDataReader dr = _db.GetQueryResult(sql);
                        if (dr.HasRows)
                        {
                            dr.Read();
                            iLevelId = dr["lvid"].ToString();
                        }
                        dr.Close();

                        if (topId != "0" && iLevelId == "001")
                        {
                            iLevelId = topId + iLevelId;
                        }

                        sql = "Insert into ANALYZE_WORD_LEVEL (LevelId, TopLevelId, LevelName, LevelSeq, DelYn) values (";
                        sql += "'" + iLevelId + "', '" + topId + "', '" + levelName + "', (Select isnull(max(LevelSeq), 0) + 1 from ANALYZE_WORD_LEVEL where topLevelId = '" + topId + "'), 'N')";
                        dr = _db.GetQueryResult(sql);
                        dr.Close();
                    }
                    // 수정
                    else
                    {
                        String sql = "Update ANALYZE_WORD_LEVEL set LevelName = '" + levelName + "' where LevelId = '" + levelId + "'";
                        SqlDataReader dr = _db.GetQueryResult(sql);
                        dr.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                succ = "N";
            }
            finally {
                result.Add("succ", succ);
            }
            Response.AddHeader("'Content-type", "application/json");
            Response.Write(getJson(result));
            return;
        }

        // 순서 변경
        if (action == "modSeq") {
            Hashtable result = new Hashtable();
            String succ = "Y";
            String datas = CheckNull(Request["datas"]);

            string[] dataArr = datas.Split('|');

            try
            {
                for (int i = 0; i < dataArr.Length; i++) {
                    string data = dataArr[i];

                    if (!isEmpty(data)) {
                        string id = data.Split(',')[0];
                        string seq = data.Split(',')[1];

                        String sql = "Update ANALYZE_WORD_LEVEL set LevelSeq = " + seq + " where LevelId = '" + id + "'";
                        SqlDataReader dr = _db.GetQueryResult(sql);
                        dr.Close();
                    }
                }   
            }
            catch (Exception ex)
            {
                succ = "N";
            }
            finally
            {
                result.Add("succ", succ);
            }
            Response.AddHeader("'Content-type", "application/json");
            Response.Write(getJson(result));
            return;
        }

        // 삭제
        if (action == "delLevel")
        {
            Hashtable result = new Hashtable();
            String succ = "Y";
            String id = CheckNull(Request["LevelId"]);

            id = RemoveSpecialChars(id);

            try
            {
                String sql = "Update ANALYZE_WORD_LEVEL set DelYn = 'Y' where LevelId = '" + id + "'";
                SqlDataReader dr = _db.GetQueryResult(sql);
                dr.Close();
            }
            catch (Exception ex)
            {
                succ = "N";
            }
            finally
            {
                result.Add("succ", succ);
            }
            Response.AddHeader("'Content-type", "application/json");
            Response.Write(getJson(result));
            return;
        }
    }

    protected void Page_Unload(object sender, EventArgs e)
    {
        _db.ReleaseConnection();
    }
    
}
